﻿@using System.Data;
@using MvcApplication1.Models
@model ItemEdit
@{
    ViewBag.Title = "UserEditItem";
    DataRowView item = ViewBag.item;
    int i;
    string ID;
    DataView itemkeyword = ViewBag.itemkeyword;
}
<script src="@Url.Content("~/Scripts/jquery-ui-1.8.21.custom.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/useredititem_keyword.js")" type="text/javascript"></script>
<script type="text/javascript">
    var UploadifyAuthCookie = '@(Request.Cookies[FormsAuthentication.FormsCookieName] == null ? "" : Request.Cookies[FormsAuthentication.FormsCookieName].Value)';
    var UploadifySessionId = '@Session.SessionID';
    var oldimage = '';
    $(document).ready(
    function () {
        $('#fileuploader').uploadify({
            'swf': '/Scripts/uploadify.swf',
            'uploader': 'Upload',    //由于从usermanage到此页面的链接包含两个‘/’符合，从而会造成upload_url错误，从导致文件无法上传，报io error或http error
            'formData': { 'itemid': '@item[0].ToString()', 'brandname': '@ViewBag.brandname' },
            'fileSizeLimit': '10MB',
            'debug': false,
            'fileTypeDesc': 'Image file',
            'fileTypeExts': '*.jpg;*.jpeg;*.gif;*.png',
            'buttonText': '选择上传图片',
            'multi': false,
            'onUploadStart': function (file) {
                $('#selected_file').text(file.name);
                //重复上传时，删除上一张图像
                $('#fileuploader').uploadify('settings', 'formData', { 'oldimage': oldimage, RequireUploadifySessionSync: true, SecurityToken: UploadifyAuthCookie, SessionId: UploadifySessionId });  //官网上关于settings的写法不行，要这样写才能更改formData
            },
            'onUploadSuccess': function (file, data, response) {
                var res = data.split('|');
                if (res[0] == 'ok') {
                    alert('图片上传成功');
                    $('#image').val(res[1]);
                    oldimage = res[1];
                    $('#itemimage').attr('src', '@Href("~/uploads/")' + res[1]);
                } else if (res[0] == 'Invaild file type') {
                    alert('图片格式不正确');
                } else {
                    alert('图片上传失败');
                }
            },
            'onUploadError': function (file, errorCode, errorMsg, errorString) {
                alert('The file ' + file.name + ' could not be uploaded: ' + errorString);
            }
        });
        oldimage = $('#image').val();   //when page first loads, there is already an upload image uploaded last time so assign it to oldimage
    }
);
</script>
<h2>商品编辑</h2>
@{
    using(Html.BeginForm("UserEditItem","Manage"))
    {
        @Html.Hidden("itemid", item[0].ToString());
        <table border="0">
        <tr><td>商品名称</td><td>@Html.TextBox("itemname",item[1].ToString())</td></tr>
        @*<tr><td>商品序号</td><td>@Html.TextBox("itemSN", item[2].ToString())</td></tr>*@
        @*<tr><td>图片</td><td>@Html.TextBox("image", item[3].ToString())</td></tr>*@
        @*<tr><td>添加时间</td><td>@Html.TextBox("addtime", item[4].ToString())</td></tr>*@
        <tr><td>链接地址</td><td>@Html.TextBox("linkAddr", item[5].ToString())</td></tr>
        <tr><td>价格</td><td>@Html.TextBox("price",item[6].ToString())</td></tr>
        <tr><td>商品简介</td><td>@Html.TextBox("intro",item[7].ToString())</td></tr>
        </table>
        <div>关键词</div>
            <div id="sortable">
                @for(i=0;i<itemkeyword.Count;i++)
                {
                    ID="keyword_"+itemkeyword[i][0].ToString()+"@"+itemkeyword[i][2].ToString();
                    <div class="keyword" id = "@ID">@itemkeyword[i][2].ToString()</div>
                } 
            </div>
        <div><button id="addkeyword" type="button">添加关键词</button></div>
        <div><label id="selected_file" >已上传图片</label><div id="fileuploader"></div></div>
        @Html.Hidden("keywordlist");
        @Html.Hidden("addtime", item[4].ToString());
        @Html.Hidden("image", item[3].ToString());
        @Html.Hidden("orginpagenumber", Request.QueryString["orginpagenumber"]);
        @Html.Hidden("orginsortorder",Request.QueryString["orginsortorder"]);
        @Html.Hidden("itemname_search",Request.QueryString["itemname_search"]);  
        @Html.Hidden("itemSN_search", Request.QueryString["itemSN_search"]);
        @Html.Hidden("addtime_search", Request.QueryString["addtime_search"]);
        @Html.Hidden("linkaddr_search", Request.QueryString["linkaddr_search"]);                                                                                                                                                                                                                                                                                       
        <input type="submit" value="保存" /><button id="returntolist" type="button" onclick="window.location='@Url.Action("UserManage", new
                                                                                                       {
                                                                                                           page = Request.QueryString["orginpagenumber"],
                                                                                                           sortorder=Request.QueryString["orginsortorder"],
                                                                                                           itemname=Request.QueryString["itemname_search"],
                                                                                                           itemSN=Request.QueryString["itemSN_search"],
                                                                                                           addtime=Request.QueryString["addtime_search"],
                                                                                                           linkaddr=Request.QueryString["linaddr_search"]
                                                                                                       })'">返回列表</button>
    }
    
}
@*移到右栏*@
<div class="display-label">图片</div>
<div class="display-field"><img id="itemimage" alt="image" /></div>
<script type="text/javascript">
    var imageurl = '@Href("~/uploads/")' + $('#image').val();
    $('#itemimage').attr('src', imageurl);
</script>